package org.herrdommel.webpagechecker;

import org.herrdommel.webpagechecker.pages.AboutFragment;
import org.herrdommel.webpagechecker.pages.ControllingTUDFragment;
import org.herrdommel.webpagechecker.pages.DortmundFragment;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;

public class MainActivity extends FragmentActivity implements ActionBar.OnNavigationListener {

	private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final ActionBar actionBar = getActionBar();
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

		actionBar.setListNavigationCallbacks(new ArrayAdapter<String>(actionBar.getThemedContext(),
				android.R.layout.simple_list_item_1, android.R.id.text1, new String[]{
						getString(R.string.title_section1), getString(R.string.title_section3),
						getString(R.string.title_section2)}), this);
	}

	@Override
	public void onRestoreInstanceState(final Bundle savedInstanceState) {
		if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
			getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
		}
	}

	@Override
	public void onSaveInstanceState(final Bundle outState) {
		outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
	}

	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onNavigationItemSelected(final int position, final long id) {
		final Fragment fragment;
		switch (position) {
			case 0 :
				fragment = new DortmundFragment();
				break;
			case 1 :
				fragment = new ControllingTUDFragment();
				break;
			default :
				fragment = new AboutFragment();
				break;
		}
		getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
		return true;
	}

}
